//
//  LHComposeToolBar.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/20.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

enum LHComposeToolBarType: Int {
    //图片
    case Picture = 0
    //@
    case Mention = 1
    //话题
    case Trend = 2
    //表情
    case Emoticon = 3
    //add
    case Add = 4
    
}

class LHComposeToolBar: UIStackView {
    
    //定义一个闭包，指定点击的是哪个按钮
    var didSelectedButtonClosure: ((type: LHComposeToolBarType) -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //修改布局方式
        axis = .Horizontal
        
        //修改填充方式
        distribution = .FillEqually
        
        addChildButon("compose_toolbar_picture", type: .Picture)
        addChildButon("compose_mentionbutton_background", type: .Mention)
        addChildButon("compose_trendbutton_background", type: .Trend)
        addChildButon("compose_emoticonbutton_background", type: .Emoticon)
        addChildButon("compose_add_background", type: .Add)
    
    }
    
    //添加子控件的方法
    private func addChildButon(imageName: String, type: LHComposeToolBarType) {
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        //去掉高亮效果
        button.adjustsImageWhenHighlighted = false
        addArrangedSubview(button)
    }
    
    
    //实现按钮的点击事件
    @objc private func buttonClick(button: UIButton) {
        
        let type = LHComposeToolBarType(rawValue: button.tag)!
        //执行闭包
        didSelectedButtonClosure!(type: type)
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
   
}
